#include "dominion.h"
#include "dominion_helpers.h"
#include <string.h>
#include <stdio.h>
#include <stdlib.h>
#include <assert.h>
#include <math.h>
#include <time.h>
#include "rngs.h"

#define NUM_TESTS 30
int RANDOM_PLAYER_COUNT_EVERYWHERE= rand()%(MAX_PLAYERS - 1) + 2;
int randomizeGameState(struct gameState *state){
        
       
        state->numPlayers = RANDOM_PLAYER_COUNT_EVERYWHERE; 
        
        state->whoseTurn = rand()%state->numPlayers;
        
        state->playedCardCount = rand()%MAX_HAND;
        
        int n;  
        for(n = 0; n < state->numPlayers; n++){ //fill a random hand
                state->deckCount[n] = rand()%MAX_DECK;
                state->discardCount[n] = rand()%MAX_DECK;
                state->handCount[n] = 0;
                
        }
        

        state->handCount[state->whoseTurn] = rand()%MAX_HAND;

        return 0;
}

int ramdomizeAdventurer(struct gameState *state){
        
        int card;
        int choice1, choice2, choice3;
        int handPos = 0;
        int bonus;
        int failure = 0;
        
        handPos = rand()%state->handCount[state->whoseTurn];
        state->hand[state->whoseTurn][handPos] = adventurer;

        
        int handCountBefore = state->handCount[state->whoseTurn];
        int playedCountBefore = state->playedCardCount;
        
        cardEffect(adventurer, 1, 1, 1, state, handPos, 0);
        
        
        int handCountAfter = state->handCount[state->whoseTurn];
        int playedCountAfter = state->playedCardCount;

        
        if(state->hand[state->whoseTurn][handCountBefore] == 4 || state->hand[state->whoseTurn][handCountBefore] == 5 || state->hand[state->whoseTurn][handCountBefore] == 6){
              
        } else {
                failure ++;
                printf("TEST FAILED: Second to last treasure is not a coin\n"); //Fails, one is not discarded at the end
        }

        if(state->hand[state->whoseTurn][handCountBefore + 1]== 4 || state->hand[state->whoseTurn][handCountBefore] == 5 || state->hand[state->whoseTurn][handCountBefore] == 6){
    
        } else {
                failure ++;
        printf("TEST FAILED: Last card is a treasure\n"); //Fails, one is not discarded at the end
        }

        if(handCountAfter - handCountBefore == 2){
        //do nothing
        } else {
                failure ++;
        printf("TEST FAILED: Hand Count after adventurer is %d\n", handCountAfter); //Fails, one is not discarded at the end
        }

        
        if(playedCountBefore - playedCountAfter == 0){
       
        } else {
                failure ++;
                printf("TEST FAILED: Number of cards played: %d\n", playedCountAfter); //Fails, card is not played at the end
        }       
        printf("Number of unit tests that failed: %d \n", failure);
        return failure;
}

int main() {
        srand(time(NULL));
        int total_failed = 0;
        
        int i;
        for(i = 0; i < NUM_TESTS; i++){
            
                int seed = rand()%10000;
                
                int k[10] = {adventurer, council_room, feast, gardens, mine,
                           remodel, smithy, village, baron, great_hall};

                struct gameState G;
        
                initializeGame( RANDOM_PLAYER_COUNT_EVERYWHERE, k, seed, &G);
        
                randomizeGameState(&G);
                total_failed = total_failed + ramdomizeAdventurer(&G);
                
                
        }
        
        printf("total failed tests: %d\n", total_failed);
        
        return 0;

}